package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.OutDrugBean;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author RJ
 * @date 2022/9/16 8:42
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private IUserService userService;

    @RequestMapping("login")
    public UserBean login(String userName, String userPwd, HttpServletRequest request) {
        UserBean user = userService.login(userName, userPwd);
        HttpSession session = request.getSession();
        if (user != null) {
            session.setAttribute("loginUser", user);
        }
        if (user != null && (user.getUserGrade().equals("超级管理员") || user.getUserGrade().equals("库房管理员"))) {
            Map<Integer, OutDrugBean> map = new HashMap<>();
            session.setAttribute("map", map);
        }
        return user;
    }

    @RequestMapping("getUser")
    public UserBean getLoginUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (UserBean) session.getAttribute("loginUser");
    }

    @RequestMapping("add")
    public String addUser(UserBean user) {
        userService.addUser(user);
        return "ok";
    }

    @RequestMapping("del")
    public String delUser(Integer userId) {
        userService.delUser(userId);
        return "ok";
    }

    @RequestMapping("update")
    public String updUser(Integer userId, String userPwd, String userGrade) {
        userService.updUser(userId, userPwd, userGrade);
        return "ok";
    }

    @RequestMapping("info")
    public IPage<UserBean> cutUsersByGrade(Integer pageNo, Integer pageSize, String userGrade) {
        IPage<UserBean> page = userService.cutUsersByGrade(pageNo, pageSize, userGrade);
        return page;
    }
}
